// @ts-ignore
import { nextHourlyTime } from "@/utils/timeTool.js";
// @ts-ignore
import dayjs from "dayjs";

import { useCommonStore } from "@/store";

// 沟通目标类型
export enum CommunicationTargetType {
  Opportunity = 0, //商机
  Customer = 1 //客户
}

export interface VisitInfo {
  /// 会议标题
  subject: string;

  /// 开始日期|时间
  upcomingStartTime: string;

  /// 开始日期|时间
  upcomingEndTime?: string;

  /// TODO: 人员模型
  participantList?: Array<Participant>;

  /// 会议地点
  location: string;

  /// 会议描述
  description: string;

  /// 在线会议链接
  onlineMeetUrl: string;

  /// 沟通客户id
  customerId: string;

  /// 沟通客户
  customerName: string;

  /// 商机id
  opportunityId: string;

  /// 沟通环节id
  stageId: string;

  /// 环节名称
  stageName: string;

  //  ---------------------------编辑沟通需要字段--------------------------------------------------------

  scheduleId?: string;

  //  ---------------------------客户端本地使用--------------------------------------------------------
  /// 沟通目标类型
  targetType: CommunicationTargetType;

  /// 商机名称
  opportunityName: string;

  /// 沟通流程id
  flowId: string;

  /// 商机的客户id
  customerIdOfOpportunity: string;
}

export interface Participant {
  /// 姓名
  name: string;

  /// sso用户id  [内部用户]
  ssoUserId?: string;

  /// 联系人id 【】9
  contactId?: string;

}

export interface FileInfo {
  name: string;
  path: string;
  downloadPath: string;
}

// 创建初始化访问信息
export const createDefaultVisitInfo = (): VisitInfo => {

  const commonStore = useCommonStore();

  const tStartTime = nextHourlyTime(dayjs(), "YYYY-MM-DD HH:mm:ss");
  const tEndTime = nextHourlyTime(dayjs().add(1, "hour"), "YYYY-MM-DD HH:mm:ss");
  const v: VisitInfo = {
    subject: "",
    targetType: commonStore.enableBusinessOpportunity ? CommunicationTargetType.Opportunity : CommunicationTargetType.Customer,
    upcomingStartTime: tStartTime as string,
    upcomingEndTime: tEndTime as string,
    participantList: [],
    customerName: "",
    stageName: "",
    onlineMeetUrl: "",
    description: "",
    location: "",
    opportunityId: "",
    opportunityName: "",
    flowId: "",
    customerId: "",
    stageId: "",
  };
  return v;
};

export const makeVisitInfoFromJson = (
  data: any,
  scheduleId: string
): VisitInfo => {
  const targetType = data.opportunityId ? CommunicationTargetType.Opportunity : CommunicationTargetType.Customer;
  let isOpportunity = targetType === CommunicationTargetType.Opportunity;
  const v: VisitInfo = {
    subject: data.subject,
    upcomingStartTime: data.upcomingStartTime,
    upcomingEndTime: data.upcomingEndTime,
    onlineMeetUrl: data.onlineMeetUrl,
    description: data.description,
    location: data.location,
    participantList: data.participantList,
    scheduleId: scheduleId,
    opportunityId: data.opportunityId,
    customerId: data.customerId,
    stageId: data.stageId,
    stageName: data.stageName,
    targetType: targetType,
  };

  if (isOpportunity) {
    v.opportunityName = data.opportunityName;
  } else {
    v.customerName = data.customerName;
  }

  return v;
};
